﻿var muserdepartmentpost = {
    //列表初始化
    gridInit: function () {
        $("#muserdepartmentpost-grid").datagrid({
            url: "/manage/userdepartmentpost/grid",
            nowrap: false, border: false,
            fit: true, singleSelect: true,
            loadMsg: "正在处理。。。",
            idField: "ID", toolbar: "#muserdepartmentpost-tools",
            columns:
            [[
                {
                    field: "Title", title: "机构职能", width: 228, align: "center",
                    formatter: function (value, row, index) {
                        try {
                            return row.BasicDepartment.Name + " - " + row.BasicPost.Name;
                        }
                        catch (e) { return ""; }
                    }
                }
            ]]
        });
    },

    //添加
    insert: function () {
        var row = $("#muser-grid").datagrid("getSelected");
        if (row) {
            muserdepartmentpost.mutual("添加", null, row.ID);
        }
        else { manage.slide("请选择用户!"); }
    },
    //修改
    update: function () {
        var row = $("#muserdepartmentpost-grid").datagrid("getSelected");
        if (row) {
            muserdepartmentpost.mutual("修改", row.ID, null);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#muserdepartmentpost-grid").datagrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/userdepartmentpost/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) {
                                muserdepartmentpost.reload();
                            }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id, userid) {
        var href = "/manage/userdepartmentpost/page?" + $.param({ id: id, userid: userid });
        var d = manage.dialog({
            title: title, href: href, width: 880,
            handler: function () {
                var deptrow = $("#muserdepartmentpost-department-grid").treegrid("getSelected");
                if (deptrow) {
                    $("#muserdepartmentpost-departmentid").val(deptrow.ID);
                }
                else { manage.msg("请选择组织机构!"); return; }
                var postrow = $("#muserdepartmentpost-post-grid").datagrid("getSelected");
                if (postrow) {
                    $("#muserdepartmentpost-postid").val(postrow.ID);
                }
                else { manage.msg("请选择赋予职能!"); return; }
                manage.form({
                    dialog: d, formid: "#muserdepartmentpost-model",
                    url: "/manage/userdepartmentpost/save",
                    success: function (result) { muserdepartmentpost.reload(); }
                });
            },
            onLoad: function () { muserdepartmentpost.formInit(id); }
        });
    },
    //弹窗初始化
    formInit: function (id) {
        $("#muserdepartmentpost-department-grid").treegrid({
            url: "/manage/department/grid",
            nowrap: false, rownumbers: true,
            fit: true, singleSelect: true, border: false,
            idField: "ID", treeField: "Name", parentField: "Pid",
            columns:
            [[
                { field: "Name", title: "名称", width: 222, align: "left" }
            ]],
            onLoadSuccess: function (row, data) {
                if (id) {
                    $("#muserdepartmentpost-department-grid").treegrid("select",
                        $("#muserdepartmentpost-departmentid").val());
                }
            }
        });
        $("#muserdepartmentpost-post-grid").datagrid({
            url: "/manage/post/allgrid",
            nowrap: false, rownumbers: true, border: false,
            fit: true, singleSelect: true, idField: "ID",
            columns:
            [[
                { field: "Name", title: "名称", width: 160, align: "center" },
                { field: "Remark", title: "描述", width: 305, align: "center" }
            ]],
            onLoadSuccess: function (data) {
                if (id) {
                    $("#muserdepartmentpost-post-grid").datagrid("selectRecord",
                        $("#muserdepartmentpost-postid").val());
                }
            }
        });
    },

    //刷新列表
    reload: function () {
        $("#muserdepartmentpost-grid").datagrid("reload");
        $("#muserdepartmentpost-grid").datagrid("clearChecked");
    }
};

$(function () { muserdepartmentpost.gridInit(); });